package com.weipt;

import com.weipt.entity.User;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.query.*;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @program: Learning
 * @description: 测试类
 * @Company: 终生学习
 * @author: peter.wei
 * @create: 2020-05-25 16:39
 **/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = RunApp.class)
public class EsOperator {


    @Autowired
    ElasticsearchRestTemplate restTemplate;

    // 给ES索引（保存）一个文档
    @Test
    public void save() {
        User user = new User();
        user.setId(10);
        user.setTitle("好消息");
        user.setAuthor("张三");
        user.setContent("Hello Word");
        IndexQuery indexQuery = new IndexQueryBuilder()
                .withIndexName("test")
                .withType("user")
                .withId(user.getId().toString())
                .build();
        // 存入索引，返回文档ID
        String documentId = restTemplate.index(indexQuery);
        System.out.println(documentId);
    }

    @Test
    public void get(){
        Map<String, Object> test = restTemplate.getSetting("test");
        System.out.println(test);
    }

    @Test
    public  void  delete(){
        String deleteId = restTemplate.delete("test", "user", "1");
        System.out.println(deleteId);
    }

    @Test
    public void getViaArgs(){
        List<String> ids = new ArrayList<String>();
        ids.add("1");
        SearchQuery query = new NativeSearchQueryBuilder()
                .withIds(ids)
                .build();
        List<User> users = restTemplate.multiGet(query, User.class);
        System.out.println(users);
    }

    @Test
    public void queryPage(){
        List<String> ids = new ArrayList<String>();
        ids.add("1");
        Pageable pageable = PageRequest.of(0,10);
        SearchQuery query = new NativeSearchQueryBuilder()
                .withIndices("test")
                .withIds(ids)
                .withPageable(pageable)
                .build();
        AggregatedPage<User> users = restTemplate.queryForPage(query, User.class);
        System.out.println(users);

    }

    @Test
    public void getEid() {
        QueryBuilder queryBuilder = QueryBuilders
                .multiMatchQuery("上海北部广告有限公司分公司", "company_name");
        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(queryBuilder)
                .withIndices("db_enterprise").withTypes("t_telephones")
                .build();
        List<User> esDoc = restTemplate.queryForList(searchQuery, User.class);
        System.out.println(esDoc);
    }
}